package com.china.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;

import com.china.utils.ActivityCollector;
import com.china.utils.LogUtil;


public abstract class BaseActivity extends ActionBarActivity {

  private final static String TAG = "BaseActivity";
  private HomeWatcherReceiver homeKeyReceiver;
  private static boolean isPresseHomeKey = false;
  private View rootView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.rootView = View.inflate(this, this.loadWindowLayout(), null);
    setContentView(this.rootView);
    ActivityCollector.addActivity(this);
    this.initTitleBar();
    this.initViews();
  }

  protected abstract int loadWindowLayout();

  protected abstract void initTitleBar();

  protected abstract void initViews();

  @Override
  public void onResume() {
    super.onResume();
    registerHomeKeyReceiver();
    // 如果isPresseHomeKey=true, 说明用户按了home键退出了
    if (isPresseHomeKey) {
      isPresseHomeKey = false;
      // 检查应用是否有更新
      this.checkAppNewVersion();
    }
  }

  @Override
  public void onPause() {
    super.onPause();
  }

  @Override
  protected void onStop() {
    super.onStop();
    unregisterHomeKeyReceiver();
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    ActivityCollector.removeActivity(this);
  }

  /**
   * 检查应用是否有更新
   */
  private void checkAppNewVersion() {}

  public static void setIsPresseHomeKey(boolean isPressedHomeKey) {
    isPresseHomeKey = isPressedHomeKey;
  }


  private final void registerHomeKeyReceiver() {
    LogUtil.i(TAG, "--------- registerHomeKeyReceiver");
    homeKeyReceiver = new HomeWatcherReceiver();
    final IntentFilter homeFilter = new IntentFilter();
    homeFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
    registerReceiver(homeKeyReceiver, homeFilter);
  }

  private final void unregisterHomeKeyReceiver() {
    LogUtil.i(TAG, "--------- unregisterHomeKeyReceiver");
    if (null != homeKeyReceiver) {
      unregisterReceiver(homeKeyReceiver);
      homeKeyReceiver = null;
    }
  }

  private static class HomeWatcherReceiver extends BroadcastReceiver {

    private static final String LOG_TAG = "HomeReceiver";
    private static final String SYSTEM_DIALOG_REASON_KEY = "reason";
    private static final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
    private static final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
    private static final String SYSTEM_DIALOG_REASON_LOCK = "lock";
    private static final String SYSTEM_DIALOG_REASON_ASSIST = "assist";

    @Override
    public void onReceive(Context context, Intent intent) {
      String action = intent.getAction();
      if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
        String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
        LogUtil.i(LOG_TAG, "----- reason: " + reason);
        if (SYSTEM_DIALOG_REASON_HOME_KEY.equals(reason) // 短按Home键
            || SYSTEM_DIALOG_REASON_RECENT_APPS.equals(reason) // 长按Home键或者activity切换键
            || SYSTEM_DIALOG_REASON_LOCK.equals(reason) // 锁屏
            || SYSTEM_DIALOG_REASON_ASSIST.equals(reason)) {// samsung长按Home键用户按了home键
          isPresseHomeKey = true;
          System.currentTimeMillis();
        }
      }
    }
  }
}
